package com.feilx.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.pojo.SkuInfo;
import com.changgou.pojo.TbSkuEntity;
import com.feilx.changgou.search.client.SkuClient;
import com.feilx.changgou.search.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsServiceImpl implements EsService {

    @Resource
    private SkuClient skuClient;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @Override
    public void importAll() {
        Map<String,Object> map = new HashMap();
        map.put("status","1");
        List<SkuInfo> all = skuClient.getAll(map);
        all.stream().forEach(i ->{
           i.setPrice(i.getPrice());
           i.setSpecMap(JSON.parseObject(i.getSpec(),Map.class));

        });
        elasticsearchRestTemplate.save(all);
    }
}
